import {Loading} from 'element-ui';

const loadding_factory = function (ajax) {
  return async function (url, data, config = {loaded: 'loaded'}) {
    const { loaded } = config
    this[loaded] = null // 加载中
    try {
      const result = await ajax.call(this, url, data, config)
      this.$nextTick(x => this[loaded] = true) // 加载成功
      return result
    } catch (e) {
      this.$nextTick(x => this[loaded] = false) // 加载失败
      throw e
    } finally {
      // 加载成功 或 失败
    }
  }
}

const status_factory = function (ajax) {
  return async function (url, data, name = 'loadStatus') {
    this[name] = null // 加载中
    try {
      const result = await ajax.call(this, url, data)
      this[name] = true // 加载成功
      return result
    } finally {
      this[name] = false // 出错或加载失败
    }
  }
}

export {
  loadding_factory,
  status_factory
}
